package com.centling.smartSealForPhone.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.activity.AuditorFirstInstanceActivity;
import com.centling.smartSealForPhone.activity.AuditorReviewActivity;
import com.centling.smartSealForPhone.activity.AuditorSealDetailActivity;
import com.centling.smartSealForPhone.bean.AuditorSealBean;
import com.centling.smartSealForPhone.constants.SPConstants;
import com.centling.smartSealForPhone.utils.ImageUtil;
import com.centling.smartSealForPhone.utils.SPUtil;
import com.centling.smartSealForPhone.view.RoundImgView;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 *
 * @author Jessie
 * @date 2016/8/16
 */
public class AuditorSealManagerAdapter extends RecyclerView.Adapter<AuditorSealManagerAdapter.AuditorHolder> {
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
    private Context context;
    private List<AuditorSealBean> auditorSealListBean;

    public AuditorSealManagerAdapter(Context context, List<AuditorSealBean> auditorSealListBean) {
        this.context = context;
        this.auditorSealListBean = auditorSealListBean;
    }

    @Override
    public AuditorSealManagerAdapter.AuditorHolder onCreateViewHolder(ViewGroup parent,
                                                                      int viewType) {
        return new AuditorHolder(
                LayoutInflater.from(context).inflate(R.layout.item_auditor_seal, parent, false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(AuditorSealManagerAdapter.AuditorHolder holder, final int position) {

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, AuditorSealDetailActivity.class).putExtra("sealApplyId", auditorSealListBean.get(position).getSealApplyId())
                        .putExtra("AuditorOperate", auditorSealListBean.get(position).getIsButtonShow()));
            }
        });
        holder.operator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, AuditorFirstInstanceActivity.class).putExtra("sealApplyId", auditorSealListBean.get(position).getSealApplyId()));
            }
        });
        holder.operatorR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, AuditorReviewActivity.class).putExtra("sealApplyId", auditorSealListBean.get(position).getSealApplyId()));
            }
        });
        switch (Integer.parseInt(auditorSealListBean.get(position).getStatus())) {
            case 0:
                holder.applyStatus.setTextColor(ContextCompat.getColor(context, R.color.history_state_0));
                break;
            case 1:
                holder.applyStatus.setTextColor(ContextCompat.getColor(context, R.color.history_state_1));
                break;
            case 2:
                holder.applyStatus.setTextColor(ContextCompat.getColor(context, R.color.history_state_2));
                break;
            case 3:
                holder.applyStatus.setTextColor(ContextCompat.getColor(context, R.color.history_state_3));
                break;
            case 4:
                holder.applyStatus.setTextColor(ContextCompat.getColor(context, R.color.history_state_4));
                break;
            case 5:
                holder.applyStatus.setTextColor(ContextCompat.getColor(context, R.color.history_state_5));
                break;
            case 6:
                holder.applyStatus.setTextColor(ContextCompat.getColor(context, R.color.history_state_6));
                break;
            case 7:
                holder.applyStatus.setTextColor(ContextCompat.getColor(context, R.color.history_state_7));
                break;
            case 8:
                holder.applyStatus.setTextColor(ContextCompat.getColor(context, R.color.history_state_8));
                break;
            case 9:
                holder.applyStatus.setTextColor(ContextCompat.getColor(context, R.color.history_state_9));
                break;
            case 10:
                holder.applyStatus.setTextColor(ContextCompat.getColor(context, R.color.history_state_10));
                break;
            default:
                break;
        }
        holder.applyStatus.setText(auditorSealListBean.get(position).getStatusInfo());
        ImageUtil.loadAvatarImage(auditorSealListBean.get(position).getApplicantUserAvatar(), holder.avatar, R.drawable.iv_avatar_default);
        if ("COUNT_LIMIT".equals(auditorSealListBean.get(position).getApplyType())) {
            holder.limitTimes.setVisibility(View.VISIBLE);
            holder.limitTime.setVisibility(View.GONE);
            if ("0".equals(auditorSealListBean.get(position).getIsButtonShow())) {
                holder.applyTimes.setVisibility(View.VISIBLE);
                holder.sealTimes.setVisibility(View.GONE);
                holder.times.setText(auditorSealListBean.get(position).getApplyTimes() + "次");
            } else {
                holder.applyTimes.setVisibility(View.GONE);
                holder.sealTimes.setVisibility(View.VISIBLE);
                holder.times.setText(auditorSealListBean.get(position).getRemainTimes() + "/" + auditorSealListBean.get(position).getApplyTimes() + "次");
            }

        } else {
            holder.limitTimes.setVisibility(View.VISIBLE);
            holder.limitTime.setVisibility(View.GONE);
            holder.applyTimes.setVisibility(View.GONE);
            holder.sealTimes.setVisibility(View.VISIBLE);
            holder.times.setText("");
            holder.sealTimes.setText("");
            holder.times.setText(String
                    .format(Locale.CHINA, "%s至%s", dateFormat.format(Long.parseLong(auditorSealListBean.get(position).getApplyStartTime())).split(" ")[0],
                            dateFormat.format(Long.parseLong(auditorSealListBean.get(position).getApplyEndTime())).split(" ")[0]));
        }
        holder.name.setText(auditorSealListBean.get(position).getApplicantUserName());
        holder.sealName.setText(auditorSealListBean.get(position).getSealName());
        if ("0".equals(auditorSealListBean.get(position).getIsButtonShow())) {
            holder.applyStatus.setVisibility(View.GONE);
            holder.operator.setVisibility(View.VISIBLE);
            holder.applyTimes.setVisibility(View.VISIBLE);
            holder.sealTimes.setVisibility(View.GONE);
            holder.operatorR.setVisibility(View.GONE);
        } else if ("1".equals(auditorSealListBean.get(position).getIsButtonShow())) {
            holder.applyStatus.setVisibility(View.GONE);
            holder.operator.setVisibility(View.GONE);
            holder.applyTimes.setVisibility(View.GONE);
            holder.sealTimes.setVisibility(View.VISIBLE);
            holder.operatorR.setVisibility(View.VISIBLE);
        } else {
            holder.applyStatus.setVisibility(View.VISIBLE);
            holder.operator.setVisibility(View.GONE);
            holder.operatorR.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(
                auditorSealListBean.get(position).getCurTaster()) && !auditorSealListBean.get(
                position).isCurUserIsCurTaster()) {
            holder.operator.setVisibility(View.GONE);
            holder.operatorR.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return auditorSealListBean.size();
    }

    class AuditorHolder extends RecyclerView.ViewHolder {
        RoundImgView label, avatar;
        TextView applyStatus, operator, operatorR, name, sealName, times, startTime, endTime, applyTimes, sealTimes;
        LinearLayout limitTime, limitTimes;

        AuditorHolder(View itemView) {
            super(itemView);
            avatar = itemView.findViewById(R.id.riv_auditor_avatar);
            label = itemView.findViewById(R.id.riv_auditor_label);
            name = itemView.findViewById(R.id.tv_auditor_apply_name);
            sealName = itemView.findViewById(R.id.tv_auditor_seal_name);
            applyStatus = itemView.findViewById(R.id.tv_auditor_apply_status);
            operator = itemView.findViewById(R.id.tv_auditor_operator_first);
            operatorR = itemView.findViewById(R.id.tv_auditor_operator_review);
            times = itemView.findViewById(R.id.tv_auditor_apply_seal_times);
            startTime = itemView.findViewById(R.id.tv_auditor_apply_seal_start_time);
            endTime = itemView.findViewById(R.id.tv_auditor_apply_seal_end_time);
            limitTime = itemView.findViewById(R.id.ll_auditor_limit_time);
            limitTimes = itemView.findViewById(R.id.ll_auditor_limit_times);
            applyTimes = itemView.findViewById(R.id.tv_auditor_apply_times_first);
            sealTimes = itemView.findViewById(R.id.tv_auditor_seal_times_first);
        }
    }
}